package library.frame;

import library.logic.Post;

import javax.swing.table.AbstractTableModel;
import java.util.Vector;

/**
 * User: RockCrazy
 * Date: 20.01.12
 * Time: 21:25
 */
public class PostTableModel extends AbstractTableModel
{
	private Vector posts;

	public PostTableModel(Vector posts)
	{
		this.posts = posts;
	}


	public void setPosts(Vector posts)
	{
		this.posts = posts;
	}

	public Post getPost(int rowIndex)
	{
		if (posts != null)
		{
			if (rowIndex < posts.size() && rowIndex >= 0)
			{
				return (Post) posts.get(rowIndex);
			}
		}
		return null;
	}

	public int getRowCount()
	{
		if (posts != null)
		{
			return posts.size();
		}
		return 0;
	}

	public int getColumnCount()
	{
		return 4;
	}


	public String getColumnName(int column)
	{
		String[] colNames = {"№ пп", "Наименование", "Адрес", "Телефон", "URL"};
		return colNames[column];
	}

	public Object getValueAt(int rowIndex, int columnIndex)
	{
		if (posts != null)
		{
			// Получаем из вектора темы
			Post post = (Post) posts.get(rowIndex);
			// В зависимости от колонки возвращаем ID, Наименование
			switch (columnIndex)
			{
				case 0:
					return post.getPostId();
				case 1:
					return post.getPostName();
				case 2:
					return post.getPostAdress();
				case 3:
					return post.getPostPhone();
				case 4:
					return post.getPostURL();
			}
		}
		return null;
	}
}
